<?php

namespace App\Admin\Controllers;

use App\Department;
use App\DepartmentUser;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class DepartmentUserController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '部门账号';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new DepartmentUser);
        $grid->disableExport();
        $grid->actions(function ($actions) {
            $actions->disableView(); // 去掉查看
        });
        $grid->model()->orderBy('id','desc');
        $list = Department::pluck('department_name', 'id')->toArray();

        $grid->column('id', __('Id'));
        $grid->column('username', '用户名称');
        $grid->column('password', '登录密码');
        $grid->column('depart_id','所属部门')->select($list);
        $grid->column('created_at','创建时间');
        $grid->column('updated_at', '更新时间');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(DepartmentUser::findOrFail($id));


        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new DepartmentUser);

        $form->text('username', '用户名称');
        $form->password('password', '登录密码');
        $list = Department::pluck('department_name', 'id')->toArray();
        $form->select('depart_id', '所属部门')->options($list);
        $form->saving(function (Form $form){
            if($form->password != $form->model()->password){
                $form->password = md5(md5($form->password));
            }
        });

        return $form;
    }
}
